package com.framework.model.login;

import com.framework.common.util.other.NumeralUtil;
import com.framework.model.system.vo.SystemUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.model.login
 * @description 自定义用户实体类
 * @datetime 2019-01-09 11:11:11
 */
public class UserPrincipal implements UserDetails, Serializable {
    private static final long serialVersionUID = -1L;
    //用户信息实体类
    private SystemUserVo systemUserVo;

    public UserPrincipal(SystemUserVo systemUserVo) {
        this.systemUserVo = systemUserVo;
    }

    public SystemUserVo getSystemUserVo() {
        return systemUserVo;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (StringUtils.isEmpty(this.systemUserVo.getRoleCode())) {
            return null;
        }
        List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
        GrantedAuthority role = new SimpleGrantedAuthority(systemUserVo.getRoleCode());
        roles.add(role);
        return roles;
    }

    @Override
    public String getPassword() {
        return systemUserVo.getPassword();
    }

    @Override
    public String getUsername() {
        return systemUserVo.getAccount();
    }

    /**
     * @return boolean true=未启动|false=启动
     * @title 验证帐号是否过期
     * @description 验证帐号是否过期
     * @author 龘鵺
     * @datetime 2025-01-09 11:11:11
     */
    @Override
    public boolean isAccountNonExpired() {
//        return systemUserVo.getAccountExpired().intValue() == NumeralUtil.POSITIVE_ONE;
//        return systemUserVo.getAccountExpired().intValue() == NumeralUtil.POSITIVE_TWO;
        return false;
    }

    /**
     * @return boolean true=未启动|false=启动
     * @title 验证是否锁住
     * @description 验证是否锁住
     * @author 龘鵺
     * @datetime 2025-01-09 11:11:11
     */
    @Override
    public boolean isAccountNonLocked() {
//        return systemUserVo.getAccountLocked().intValue() == NumeralUtil.POSITIVE_ONE;
//        return systemUserVo.getAccountLocked().intValue() == NumeralUtil.POSITIVE_TWO;
        return false;
    }

    /**
     * @return boolean true=未启动|false=启动
     * @title 验证凭证是否过期
     * @description 验证凭证是否过期
     * @author 龘鵺
     * @datetime 2025-01-09 11:11:11
     */
    @Override
    public boolean isCredentialsNonExpired() {
//        return systemUserVo.getCredentialsExpired().intValue() == NumeralUtil.POSITIVE_ONE;
//        return systemUserVo.getCredentialsExpired().intValue() == NumeralUtil.POSITIVE_TWO;
        return false;
    }

    /**
     * @return boolean true=未启动|false=启动
     * @title 验证启用状态
     * @description 验证启用状态
     * @author 龘鵺
     * @datetime 2025-01-09 11:11:11
     */
    @Override
    public boolean isEnabled() {
        return systemUserVo.getEnableStatus().intValue() == NumeralUtil.POSITIVE_TWO;
    }

    /**
     * @param o 1 自定义用户实体类
     * @return boolean
     * @title 重写eq方法用于单点登录验证，不然判断会出现匹配不上BUG
     * @description 重写eq方法用于单点登录验证，不然判断会出现匹配不上BUG
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserPrincipal that = (UserPrincipal) o;

        return systemUserVo != null ? systemUserVo.equals(that.systemUserVo) : that.systemUserVo == null;
    }

    /**
     * @return int
     * @title 重写hashCode用户单点登录验证，不然判断出出现匹配不上BUG
     * @description 重写hashCode用户单点登录验证，不然判断出出现匹配不上BUG
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @Override
    public int hashCode() {
        return systemUserVo != null ? systemUserVo.hashCode() : 0;
    }
}
